package cn.ycxy.lbl.posts.controller;

import cn.hutool.core.util.IdUtil;
import cn.ycxy.lbl.common.exception.BusinessException;
import cn.ycxy.lbl.common.exception.enums.ResponseEnum;
import cn.ycxy.lbl.common.restful.R;
import cn.ycxy.lbl.pojo.order.domain.LblCsOrder;
import cn.ycxy.lbl.pojo.order.dto.OrderListDTO;
import cn.ycxy.lbl.pojo.order.dto.OrderUpdateDTO;
import cn.ycxy.lbl.pojo.order.vo.OrderListVO;
import cn.ycxy.lbl.posts.service.IOrderService;
import cn.ycxy.lbl.posts.utils.SecurityContextUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@Api(tags = "订单模块")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;


    @PreAuthorize("hasRole('user')")
    @ApiOperation(value = "获取我购买到的订单数据")
    @GetMapping("/getBuy")
    public R<List<OrderListVO>> getBuy(@Validated OrderListDTO orderListDTO){
        Long userId = SecurityContextUtils.getUserId();
        orderListDTO.setUserId(userId);
        if (orderListDTO.getState() == -1){
            orderListDTO.setState(null);
        }
        return R.ok(orderService.selectOrderByBuy(orderListDTO));
    }

    @PreAuthorize("hasRole('user')")
    @ApiOperation(value = "获取我卖出的订单数据")
    @GetMapping("/sale")
    public R<List<OrderListVO>> saleOrder(@Validated OrderListDTO orderListDTO){
        Long userId = SecurityContextUtils.getUserId();
        orderListDTO.setUserId(userId);
        if (orderListDTO.getState() == -1){
            orderListDTO.setState(null);
        }
        return R.ok(orderService.selectOrderBySale(orderListDTO));
    }

    @PreAuthorize("hasRole('user')")
    @ApiOperation(value = "提交订单")
    @PostMapping("/commit")
    public R<Void> commitOrder(@Validated @RequestBody LblCsOrder order){
        Long userId = SecurityContextUtils.getUserId();
        QueryWrapper<LblCsOrder> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("post_id", order.getPostId());
        qw.ne("state", 3);
        if (orderService.count(qw) > 0){
            throw new BusinessException(ResponseEnum.ORDER_COMMIT_FAILED);
        }
        order.setUserId(userId);
        //生成订单编号
        order.setSn(IdUtil.simpleUUID());
        order.setState(0);
        orderService.save(order);
        return R.ok();
    }

    @PreAuthorize("hasRole('user')")
    @ApiOperation(value = "更新订单状态")
    @PostMapping("/modify")
    public R<Void> updateOrder(@Validated @RequestBody OrderUpdateDTO order){
        LblCsOrder lblCsOrder = new LblCsOrder();
        BeanUtils.copyProperties(order, lblCsOrder);
        if (order.getState() == 1){
            //设置发货时间
            lblCsOrder.setSendTime(LocalDateTime.now());
        }else if (order.getState() == 2){
            //设置交易完成时间
            lblCsOrder.setFinishTime(LocalDateTime.now());
        }
        orderService.modifyDealState(lblCsOrder);
        return R.ok();
    }



}
